package com.shuw.com.beingfortextproject.utils;

import android.content.Context;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

/**
 * Description  保存对象到本地 data/data/目录下
 */

public class SavaObjectUtil {
	public static synchronized void storeObject(final Context context, String name, Object object) throws IOException {
		FileOutputStream os = context.openFileOutput(name, Context.MODE_PRIVATE);
		ObjectOutputStream objectOutputStream = new ObjectOutputStream(os);
		objectOutputStream.writeObject(object);
		objectOutputStream.flush();
		objectOutputStream.close();
	}
	public static synchronized Object resotreObject(final Context context, String name) throws IOException, ClassNotFoundException {
		FileInputStream fileInputStream = context.openFileInput(name);
		ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream);
		return objectInputStream.readObject();
	}

	public static void delete(final Context context, String name) throws FileNotFoundException {
		File file = new File(context.getFilesDir(),name);
		if (file!=null){
			file.delete();
		}
	}


}
